package com.linwen.javase.time;

import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.SimpleFormatter;

/**
 * @author : linwen
 * @date : 上午12:53 2021/3/3
 */
public class TestDateTime {
    /**
     *date类型的格式化和解析
     */

    @Test
    public void test(){
        //jdk1  Date
        Date date = new Date();
        System.out.println(date);
        System.out.println(date.getTime());
        //格式化
        SimpleDateFormat simpleFormatter = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleFormatter.format(date);
        System.out.println(format);
        //解析成时间格式
        String data = "2021-03-01";
        try {
            Date parse = simpleFormatter.parse(data);
            System.out.println(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    /**
     * 日历类
     */

    @Test
    public void test1(){
        Calendar instance = Calendar.getInstance();
        Date time = instance.getTime();
        System.out.println(time);

        System.out.println(instance.get(Calendar.YEAR));
        System.out.println(instance.get(Calendar.MONTH)+1);
        System.out.println(instance.get(Calendar.DATE));

        System.out.println(instance.get(Calendar.HOUR_OF_DAY));
        System.out.println(instance.get(Calendar.MINUTE));
        System.out.println(instance.get(Calendar.SECOND));

    }

    /**
     * jdk8中的日期相关类
     *
     */
    @Test
    public void test2(){
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);

        // 格式化用
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        String format = dateTimeFormatter.format(localDateTime);
        System.out.println(format);

        TemporalAccessor parse = dateTimeFormatter.parse("2021-03-07 12:12:41");
        System.out.println(parse);

    }

}
